/*
 * decode.h
 *
 *  Created on: Jun 6, 2025
 *      Author: 26474
 */

#ifndef INC_DECODE_H_
#define INC_DECODE_H_

#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include "../../iNav_lib/timeSync.h"

#define GNSS_DATA_SIZE 500

#pragma pack(push, 4)
typedef struct
{
	uint16_t head;
	uint16_t tail;
	uint16_t size;
	uint16_t dataLen;
	uint8_t  dd[GNSS_DATA_SIZE];
	uint8_t  empty_cnt;
}GNSS_RingBuffer_s;
#pragma pack(pop)
extern GNSS_RingBuffer_s GNSS_fifo;

#pragma pack(push, 1)
typedef struct
{
	uint8_t  sync[3];
	uint8_t  cpuIdle;
	uint16_t msgId;
	uint16_t msgLen;
	uint8_t  timeRef;
	uint8_t  timeStatus;
	uint16_t week;
	uint32_t itow;
	uint32_t version;
	uint8_t  reserved;
	uint8_t  leapSec;
	uint16_t delayMs;
}UM982_BINARY_HEADER_MSG_s;
extern UM982_BINARY_HEADER_MSG_s um982_bin_header_info;

typedef struct
{
	int    p_sol_status;
	int    pos_type;
	double lat;
	double lon;
	double hgt;
	float  undulation;
	int	   datumId;
	float  lat_std;
	float  lon_std;
	float  hgt_std;
	char   stnId[4];
	float  diff_age;
	float  sol_age;
	uint8_t SVs;
	uint8_t solnSVs;
	uint8_t reserved[6];
	int    v_sol_status;
	int    vel_type;
	float  latency;
	float  age;
	double hor_spd;
	double trk_gnd;
	double vert_spd;
	float  verspd_std;
	float  horspd_std;

	gpst_t gpst;
	int is_update;
}UM982_BESTNAV_s;
extern UM982_BESTNAV_s um982_bestnav;

typedef struct
{
	int    sol_stat;
	int    pos_type;
	float  length;
	float  heading;
	float  pitch;
	float  reserved;
	float  hdgstddev;
	float  ptchstddev;
	char   stn_id[4];
	uint8_t Svs;
	uint8_t solnSVs;
	uint8_t obs;
	uint8_t multi;
	uint8_t reserved1[4];

	gpst_t gpst;
	int is_update;
}UM982_UNIHEADING_s;
extern UM982_UNIHEADING_s um982_uniheading;
#pragma pack(pop)

#pragma pack(push, 4)
typedef struct
{
	uint32_t frame_cnt;
	uint32_t frame_BESTNAVB_cnt;
	uint32_t frame_UNIHEADINGB_cnt;
	uint32_t rcv_checkSum;
	uint32_t calc_checkSum;
	uint32_t check_ok_cnt;
	uint32_t check_failed_cnt;
}DECODE_Monitor_s;
#pragma pack(pop)

typedef enum
{
	HEADER1 = 0,
	HEADER2,
	HEADER3,
	HEADER_INFO,
	DATA,
	CRC32
}UM982_DECODE_BINARY_STEP_ENUM;

void um982_decode_binary_func(uint8_t rcv);

//typedef enum
//{
//	HEADER = 0,
//	FRAME_TYPE,
//	GNZDA,
//	BESTNAVA,
//	UNIHEADINGA,
//	CHECKSUM,
//	FRAME_END
//}UM982_DECODE_ASCII_STEP_ENUM;
//
//void um982_decode_ascii_func(uint8_t rcv);
//void decode_ascii_GNGDZ_func(const uint8_t *rcv, int len);
//void decode_ascii_BESTNAVA_func(const uint8_t *rcv, int len);
//void decode_ascii_UNIHEADINGA_func(const uint8_t *rcv, int len);
//
//uint8_t NMEA0183_XOR_Checksum(const char *nmea_str);
uint32_t CRC32_Checksum(const uint8_t *buf, uint16_t len);

#endif /* INC_DECODE_H_ */
